package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.WarehouseBin;
import com.fowo.api.mapper.ProductMapper;
import com.fowo.api.mapper.StockInMapper;
import com.fowo.api.mapper.WarehouseBinMapper;
import com.fowo.api.model.product.ProductVo;
import com.fowo.api.model.sales.withdraw.SalesWithdrawVo;
import com.fowo.api.model.sales.withdraw.product.SalesWithdrawProductItemVo;
import com.fowo.api.model.stock.in.StockInVo;
import com.fowo.api.model.stock.in.product.StockInProductItemVo;
import com.fowo.api.service.ProductService;
import com.fowo.api.service.SalesWithdrawService;
import com.fowo.api.service.StockInService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SalesWithdrawOrderServiceImpl implements SalesWithdrawOrderService{

    @Resource
    StockInService stockInService;
    @Resource
    StockInMapper stockInMapper;
    @Resource
    WarehouseBinMapper warehouseBinMapper;
    @Resource
    SalesWithdrawService salesWithdrawService;
    @Resource
    ProductService productService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchFastStorage(List<SalesWithdrawVo> lists) {
        for (SalesWithdrawVo s:lists) {
            Assert.isTrue("0".equals(s.getStatus())&&"0".equals(s.getHasPrediction()),s.getRmaOrderNo()+"当前单据状态不可进行此操作！");
            StockInVo stock = new StockInVo();
//            入库类型：退货入库
            stock.setReceiptType("RK003");
//            入库日期warehousingDate
            stock.setWarehousingDate(new Date());
//          入库仓库
            stock.setWarehouse(s.getWhname());
//            备注
            stock.setRemarks(s.getRmaOrderNo());
            Integer quantitys = 0;
            BigDecimal amount = new BigDecimal(0);
            List<StockInProductItemVo> sipvs = new ArrayList<>();
            for (SalesWithdrawProductItemVo pvo:s.getLists()) {
                StockInProductItemVo stockInProductItemVo = new StockInProductItemVo();
//                产品id
                stockInProductItemVo.setProductId(pvo.getProductName());
                ProductVo voById = null;
                try {
                    voById = productService.getVoById(pvo.getProductName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                产品图片
                stockInProductItemVo.setImg(voById.getPicUrl());
                //品名
                stockInProductItemVo.setProductName(voById.getName());
//                sku
                stockInProductItemVo.setSku(pvo.getSku());
//                成本价格costPrice
                stockInProductItemVo.setCostPrice(pvo.getOutboundCost().multiply(new BigDecimal(pvo.getReturnQuantity())));
//                单位入库成本
                stockInProductItemVo.setUnitReceiptCost(pvo.getOutboundCost());
//                 产品单价
                stockInProductItemVo.setUnitPrice(pvo.getOutboundCost());
//                货值
                stockInProductItemVo.setProductPrice(pvo.getOutboundCost().multiply(new BigDecimal(pvo.getReturnQuantity())));
//                入库量 quantity
                stockInProductItemVo.setQuantity(pvo.getReceivingQuantity());
                quantitys=pvo.getReceivingQuantity()+quantitys;
//                主表总金额合计
                BigDecimal multiply = pvo.getOutboundCost().multiply(new BigDecimal(pvo.getReceivingQuantity()));
                amount = amount.add(multiply);
//                良品量
                stockInProductItemVo.setGoodProduct(pvo.getAvailableQuantity());
//                次品量
                stockInProductItemVo.setDefectiveProduct(pvo.getDefectiveQuantity());
//                仓位
                Long wid =s.getWhname();
                String availableWbin = pvo.getAvailableWbin();
                String defectiveWbin = pvo.getDefectiveWbin();
                WarehouseBin warehouseBin = warehouseBinMapper.selectOne(Wrappers.lambdaQuery(WarehouseBin.class).eq(WarehouseBin::getWarehouseId, wid).eq(WarehouseBin::getType, availableWbin).eq(WarehouseBin::getStatus,"1"));
                WarehouseBin warehouseBin1 = warehouseBinMapper.selectOne(Wrappers.lambdaQuery(WarehouseBin.class).eq(WarehouseBin::getWarehouseId, wid).eq(WarehouseBin::getType, defectiveWbin).eq(WarehouseBin::getStatus,"1"));
                if(warehouseBin!=null){
                    stockInProductItemVo.setPositions(warehouseBin.getId());
                }
                if(warehouseBin1!=null){
                    stockInProductItemVo.setDefectiveProductPosition(warehouseBin1.getId());
                }
                sipvs.add(stockInProductItemVo);
            }
            stock.setProductList(sipvs);
//            总入库量
            stock.setTotalStorage(quantitys.toString());
//            金额
            stock.setTotalAmount(amount.toString());
//            完成状态
            s.setStatus("1");
            StockIn stockIn = stockInMapper.selectOne(Wrappers.lambdaQuery(StockIn.class).eq(StockIn::getRemarks,s.getRmaOrderNo()));
            Assert.isTrue(stockIn==null,s.getRmaOrderNo()+"此单已有入库单！");
            try {
                    stockInService.create(stock);
                    salesWithdrawService.update(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
